<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2018/11/30
 * Time: 14:13
 */

namespace app\mec\controller;


use think\Db;
use think\Request;

class Building extends Base
{
    /**
     * 首页展示
     * @return mixed|\think\response\Json
     */
    public function index()
    {
        if(Request::instance()->isAjax()) {
            $params = input('param.');
            $where = [];
            //名称
            if(isset($params['in_name']) && $params['in_name'] != "") {
                $where['uiw.in_name'] = ['like', "%" . $params['in_name'] . "%"];
            }
            //规格类型
            if(isset($params['spec_type_id']) && $params['spec_type_id'] != '') {
                $where['uiw.spec_type_id'] = $params['spec_type_id'];
            }
            //房屋类型
            if(isset($params['house_type_id']) && $params['house_type_id'] != '') {
                $where['uiw.house_type_id'] = $params['house_type_id'];
            }
            //仓库
            $where['uiw.warehouse_id'] = session('warehouse_id');
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;// 获取总条数;
            $count = Db::table('up_in_warehouse')->count();
            $lists = Db::table('up_in_warehouse')
                ->alias('uiw')
                ->join('up_u_user uuu', 'uuu.user_id = uiw.in_user_id', 'left')
                ->join('up_u_user uuu1', 'uuu1.user_id = uiw.check_user_id', 'left')
                ->join('up_house_type uht', 'uht.house_type_id = uiw.house_type_id', 'left')
                ->join('up_spec_type ust', 'ust.spec_type_id = uiw.spec_type_id', 'left')
                ->join('up_warehouse uw', 'uw.house_id = uiw.warehouse_id', 'left')
                ->field(['in_id', 'create_time', 'in_num', 'house_type', 'spec_type', 'warehouse_name',
                    'uuu.user_username', 'uuu1.user_username' => 'check_user', 'check_user_id',
                    'in_name', "concat('".http_type()."',in_img)"=>'in_img', 'in_status', 'in_abstract'])
                ->page($Nowpage, $limits)
                ->where($where)
                ->order('create_time desc')
                ->select();
            foreach($lists as $key => &$vo){
                if($vo['check_user_id'] == -1) $vo['check_user'] = 'admin';
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }

        //规格类型
        $specType = Db::table('up_spec_type')
            ->field('spec_type_id, spec_type')
            ->select();
        //房屋类型
        $houseType = Db::table('up_house_type')
            ->field('house_type_id, house_type')
            ->select();

        $this->assign([
            'specType' => $specType,
            'houseType' => $houseType,
        ]);
        return $this->fetch();
    }
    
    /**
     * 入库物件审核
     * @return mixed|\think\response\Json
     */
    public function audit(){
        if(Request::instance()->isPost()){
            $in_status = input('param.in_status');
            $id = input('param.in_id');
            $data = [
                'in_status' => $in_status,//状态  2 通过  2 不通过
                'check_user_id' => -1,//审核者，后台审核时为-1
                'check_time' => date('Y-m-d H:i:s'),//审核时间
            ];
            $res = Db::table('up_in_warehouse')->where('in_id', $id)->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '审核成功', 'data' => '']);
            }else{
                return json(['code' => 100, 'msg' => '审核失败', 'data' => '']);
            }
        }
        $in_id = input('param.id');
        $info = Db::table('up_in_warehouse')
            ->alias('uiw')
            ->join('up_u_user uuu', 'uuu.user_id = uiw.in_user_id', 'left')
            ->join('up_house_type uht', 'uht.house_type_id = uiw.house_type_id', 'left')
            ->join('up_spec_type ust', 'ust.spec_type_id = uiw.spec_type_id', 'left')
            ->join('up_warehouse uw', 'uw.house_id = uiw.warehouse_id', 'left')
            ->field(['in_num', 'in_name', "concat('".http_type()."',in_img)"=>'in_img',
                'in_address', 'in_abstract', 'create_time', 'user_username', 'house_type', 'spec_type',
            'warehouse_name', 'in_id'])
            ->where('in_id', $in_id)
            ->find();
        $this->assign('info', $info);
        return $this->fetch();
    }
    
    /**
     * 删除
     * @return \think\response\Json
     */
    public function delBuilding(){
        $in_id = input('param.id');
        Db::startTrans();
        try{
            Db::table('up_in_warehouse')->delete($in_id);
            Db::table('up_jia')->where('jia_ku_id', $in_id)->delete();
            Db::commit();
            $res = true;
        }catch(\Exception $e){
            Db::rollback();
            $res = false;
        }
        if($res){
            return json(['code'=>200,'msg'=>'删除成功']);
        }else{
            return json(['code'=>100,'msg'=>'删除失败']);
        }
    }

    
    /**
     * 自定义属性列表展示
     * @return mixed|\think\response\Json
     */
    public function costun(){
        if(Request::instance()->isAjax()) {
            $in_id = input('param.id');
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;// 获取总条数;
            $count = Db::table('up_jia')->where('jia_ku_id', $in_id)->count();
            $lists = Db::table('up_jia')
                ->field('jia_id, jia_name, jia_value')
                ->page($Nowpage, $limits)
                ->where('jia_ku_id', $in_id)
                ->order('jia_id desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $param = input('param.');
        if(!session('?in_id')){
            session('in_id',$param['id']);
        }
        return $this->fetch();
    }
}